package com.example.signin

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.signin.databinding.ActivityMainBinding
import com.example.signin.uitl.JWebSocketClient
import com.google.android.material.bottomnavigation.BottomNavigationView
import java.net.URI


class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    val uri: URI = URI.create("ws://192.168.1.6:5000")
    private var client: JWebSocketClient = object : JWebSocketClient(uri) {
        override fun onMessage(message: String) {
            //message就是接收到的消息
            Log.w("JWebSClientService", message)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val navView: BottomNavigationView = binding.navView

        val navController = findNavController(R.id.nav_host_fragment_activity_main)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
//        openConnect()
    }

    override fun onDestroy() {
        super.onDestroy()
//        closeConnect()
    }

    private fun openConnect(){
        try {
            Log.d("TAG", "openConnect: ==============")
            client.connectBlocking()
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }
    }


    /**
     * 断开连接
     */
    private fun closeConnect() {
        try {
            client.close()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}